package com.example.javaui.component;

import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.*;
import ohos.agp.utils.Color;
import ohos.agp.utils.Point;
import ohos.agp.utils.RectFloat;
import ohos.app.Context;

public class CustomComponent extends Component implements Component.DrawTask {

    public CustomComponent(Context context) {
        super(context);
        initView();
    }

    public CustomComponent(Context context, AttrSet attrSet) {
        super(context, attrSet);
        initView();
    }

    public CustomComponent(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        initView();
    }

    public CustomComponent(Context context, AttrSet attrSet, int resId) {
        super(context, attrSet, resId);
        initView();
    }

    private void initView() {
        this.addDrawTask(this);
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        // 绘制渐变背景
        Paint paint = new Paint();
        Point point1 = new Point(0,0);
        Point point2 = new Point(300,300);
        Point[] newPoints = new Point[]{point1, point2}; // 渐变点
        float[] newStops = new float[]{0,1}; // 渐变位置
        Color[] newColors = new Color[]{Color.WHITE, Color.BLACK}; // 渐变颜色
        Shader.TileMode tileMode = Shader.TileMode.REPEAT_TILEMODE; // 重复模式
        LinearShader shader = new LinearShader(newPoints, newStops, newColors, tileMode);
        paint.setShader(shader, Paint.ShaderType.LINEAR_SHADER);


        // 将渐变背景转换为外部阴影
        MaskFilter maskFilter = new MaskFilter(20, MaskFilter.Blur.OUTER);
        paint.setMaskFilter(maskFilter);

        // 绘制原形
        canvas.drawCircle(170, 170, 150, paint);
    }
}
